﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// DataAccess 的摘要说明

/// </summary>
public class DataAccess
{
    private DataAccess() { }
    static readonly DataAccess da = new DataAccess();
    public static DataAccess getInstance()
    {
        return da;
    }

    public DataTable Query(string sql)
    {
        string connStr = System.Configuration.ConfigurationManager.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            //try
            //{
            using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
            //}
            //catch
            //{
            //    return null;
            //}

        }
    }
    public bool ExecuteNonQuery(string sql)
    {
        string connStr = System.Configuration.ConfigurationManager.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr))
        {

            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            } conn.Close();
            return true;
            //try
            //{
            //}
            //catch
            //{
            //    return false;
            //}
            //finally
            //{
            //    conn.Close();
            //}
        }

    }
    public object ExecuteScalar(string sql)
    {
        string connStr = System.Configuration.ConfigurationManager.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr))
        {

            try
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
            catch
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
